import {PartHost} from "./PartBase"
import { conn } from '@beshell/conn-ts/Connection'
import {AppModel} from '../AppModel'
import { Stud } from '../Stud'
import { Usage } from '../StudUsage'
import { Cable } from "../conn/Cable"
import { I2C } from "../conn/I2C"
import { SPI } from "../conn/SPI"
import { UART } from "../conn/UART"
import confBeCore16 from './config/BeCore16.json'
import confBeCore32 from './config/BeCore32.json'
import confBeVison from './config/BeVision.json'
import confBePad from './config/BePad.json'

export class PartESP32 extends PartHost {

    constructor(_model: AppModel, config: any){
        super(_model, config)

        // 串口线路
        this.uartLines[0] = new UART("UART 0", "0", 115200)
        this.uartLines[1] = new UART("UART 1", "1", 115200)
        this.uartLines[2] = new UART("UART 2", "2", 115200)

        this.i2cLines[0] = new I2C("I2C 0", "0")
        this.i2cLines[1] = new I2C("I2C 1", "1")
        
        this.spiLines[0] = new SPI("SPI 0", "0")
        this.spiLines[1] = new SPI("SPI 1", "1")
        this.spiLines[1] = new SPI("SPI 2", "2")

        let listenCableSetting = (lstcables: Cable[]) => {
            for(let cable of lstcables) {
                // 更改 cable 的接口后
                cable.on('cable.set.stud', (cable:Cable, usage:Usage, stud:Stud)=>{
                    if( cable.isValid() && conn.isConnected ) {
                        cable.setup()
                    }
                })
            }
        }
        listenCableSetting(this.uartLines)
        listenCableSetting(this.i2cLines)
        listenCableSetting(this.spiLines)


        // 连接状态
        conn.on("*", (eventName:string, ...argv: any[])=>{
            switch(eventName) {
                case 'connected' :
                    this.setup()
                    break

                case 'connecting' :
                    break

                case 'disconnected' :
                    break

                case 'error' :
                    break
            }

            // 转发 connection 的事件
            this.emit(eventName, ...argv)
        })
    }
}


export class BeCore16 extends PartESP32 {
    constructor(_model: AppModel){
        super(_model, confBeCore16)
    }
}
export class BeCore32 extends PartESP32 {
    constructor(_model: AppModel){
        super(_model, confBeCore32)
    }
}
export class BePad extends PartESP32 {
    constructor(_model: AppModel){
        super(_model, confBePad)
    }
}
export class BeVision extends PartESP32 {
    constructor(_model: AppModel){
        super(_model, confBeVison)
    }
}